package hu.genius.model;

import hu.genius.model.entity.Product;

import java.util.HashMap;
import java.util.Map;

import android.util.Log;

public enum BasketManager {
	INSTANCE;
	
	private Map<Product, Integer> orderMap;

	{
		this.orderMap = new HashMap<Product, Integer>();
	}

	public Map<Product, Integer> getOrderMap() {
		return orderMap;
	}

	public void addProduct(Product product) {
		int productQuantity = 0;
		if (this.orderMap.containsKey(product)) {
			productQuantity = this.orderMap.get(product);
		}
		this.orderMap.put(product, ++productQuantity);
	}
	
	public void setProductByQuantity(Product product, int quantity) {
		if (quantity == 0) {
			this.orderMap.remove(product);
		} else {
			this.orderMap.put(product, quantity);
		}
	}
	
	public void addProductByQuantity(Product product, int quantity) {
		int productQuantity = 0;
		if (this.orderMap.containsKey(product)) {
			productQuantity = this.orderMap.get(product);
		}
		productQuantity += quantity;
		this.orderMap.put(product, productQuantity);
	}

	public void removeProduct(Product product) {
		int productQuantity = 0;
		if (this.orderMap.containsKey(product)) {
			productQuantity = this.orderMap.get(product);
		}
		if (productQuantity > 1) {
			this.orderMap.put(product, --productQuantity);
		} else {
			this.orderMap.remove(product);
		}
	}

	public int getQuantityByProductId(Product product) {
		Log.d("zs", "Get quantity by product: " + product.toString());
		int quantity = 0;
		if (this.orderMap.containsKey(product)) {
			Log.d("zs", "Found product in basket.");
			quantity = this.orderMap.get(product);
		}
		return quantity;
	}

	public void resetOrderMap() {
		this.orderMap.clear();
	}

	@Override
	public String toString() {
		return "BasketManager [orderMap=" + orderMap + "]";
	}
	
}
